﻿using System;
using Entity;
using NLog;

public partial class Admin_Default : ParentPage
{
    #region Variables
    private static Logger _logger = LogManager.GetCurrentClassLogger();
    #endregion

    #region pageload
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                Users objUser = (Users)Session["Admin"];
                if (objUser.UserPermission == 1)
                {
                    div_EmptyDiv.Style.Add("display", "");
                    tbDashbaord.Style.Add("display", "none");
                }
                else
                {
                    div_EmptyDiv.Style.Add("display", "none");
                }

                SiteConfiguration objSiteConfiguration = FormsFunction.RetrieveWebConfig();


                txtSiteName.Text = objSiteConfiguration.SiteName;
                txtSiteMetaDescription.Text = objSiteConfiguration.MetaDescription;
                txtSiteMetaKeywords.Text = objSiteConfiguration.MetaKeywords;
                ddlSiteStstus.SelectedValue = objSiteConfiguration.LogStatus;
                ddlLogStatus.SelectedValue = objSiteConfiguration.LogStatus;
                txtLogPath.Text = objSiteConfiguration.LogPath;
                ddlCasheStatus.SelectedValue = objSiteConfiguration.CacheStatus;
                txtSessionLifetime.Text = objSiteConfiguration.SessionLifetime.ToString();
                ddlDBType.SelectedValue = objSiteConfiguration.DatabaseType;
                txtDBHost.Text = objSiteConfiguration.Host;
                txtDatabaseUsername.Text = objSiteConfiguration.DatabaseUsername;
                txtDatabasePassword.Text = objSiteConfiguration.DatabasePassword;
                txtDatabaseName.Text = objSiteConfiguration.DatabaseName;
                txtFromEmail.Text = objSiteConfiguration.FromEmail;
                txtFromName.Text = objSiteConfiguration.FromName;
                ddlSMTPAuthentication.SelectedValue = objSiteConfiguration.SMTPAuthentication;
                ddlSMTPSecurity.SelectedValue = objSiteConfiguration.SMTPSecurity.ToString();
                txtSMTPPort.Text = objSiteConfiguration.SMTPPort.ToString();
                txtSMTPUsername.Text = objSiteConfiguration.SMTPUsername;
                txtSMTPPassword.Text = objSiteConfiguration.SMTPPassword;
                txtSMTPHost.Text = objSiteConfiguration.SMTPHost.ToString();
                ddlLiveStream.SelectedValue = objSiteConfiguration.IsLiveStreamEnable.ToString();
            }
        }
        catch (Exception ex)
        {
            _logger.Error("Admin_Default:::Page_Load:::" + ex.Message);
        }
    }
    #endregion

    #region Events
    protected void btnSaveSettings_Click(object sender, EventArgs e)
    {
        try
        {
            SiteConfiguration objSiteConfiguration = new SiteConfiguration();
            objSiteConfiguration.SiteName = txtSiteName.Text;
            objSiteConfiguration.MetaDescription = txtSiteMetaDescription.Text;
            objSiteConfiguration.MetaKeywords = txtSiteMetaKeywords.Text;
            objSiteConfiguration.SiteOffline = ddlSiteStstus.SelectedValue;
            objSiteConfiguration.LogStatus = ddlLogStatus.SelectedValue;
            objSiteConfiguration.LogPath = txtLogPath.Text;
            objSiteConfiguration.CacheStatus = ddlCasheStatus.SelectedValue;
            objSiteConfiguration.SessionLifetime = Convert.ToInt16(txtSessionLifetime.Text);
            objSiteConfiguration.DatabaseType = ddlDBType.SelectedValue;
            objSiteConfiguration.Host = txtDBHost.Text;
            objSiteConfiguration.DatabaseUsername = txtDatabaseUsername.Text;
            objSiteConfiguration.DatabasePassword = txtDatabasePassword.Text;
            objSiteConfiguration.DatabaseName = txtDatabaseName.Text;
            objSiteConfiguration.FromEmail = txtFromEmail.Text;
            objSiteConfiguration.FromName = txtFromName.Text;
            objSiteConfiguration.SMTPAuthentication = ddlSMTPAuthentication.SelectedValue;
            objSiteConfiguration.SMTPSecurity = int.Parse(ddlSMTPSecurity.SelectedValue);
            objSiteConfiguration.SMTPPort = int.Parse(txtSMTPPort.Text);
            objSiteConfiguration.SMTPUsername = txtSMTPUsername.Text;
            objSiteConfiguration.SMTPPassword = txtSMTPPassword.Text;
            objSiteConfiguration.SMTPHost = txtSMTPHost.Text;
            objSiteConfiguration.IsLiveStreamEnable = int.Parse(ddlLiveStream.SelectedValue);
            FormsFunction.UpdateWebConfig(objSiteConfiguration);
            hfResetPoral.Value = "true";
        }
        catch (Exception ex)
        {
            _logger.Error("Admin_Default:::btnSaveSettings_Click:::" + ex.Message);
            hfResetPoral.Value = "false";
        }
    }
    #endregion
}